import { isObj } from '@/utils/validate/data';

const processUrlParams = params => {
	if (isObj(params)) {
		if (isObj(params.query) && params.url) {
			let queryStrs = [];
			Object.keys(params.query).forEach(key => {
				queryStrs.push(`${key}=${params.query[key]}`);
			});

			if (queryStrs.length) {
				params.url = `${params.url}?${queryStrs.join('&')}`;
			}
		}
	}

	return params;
};

// 获取当前页面信息
const url = _ => {
	let pages = getCurrentPages();
	let curPage = pages[pages.length - 1];
	// 重写部分参数
	// 当前页面的路由
	curPage.url = curPage.route;

	// 当前页面的参数
	curPage.query = curPage.options || curPage.$route.query;

	return curPage;
};

url.navigateTo = params => {
	uni.navigateTo(processUrlParams(params));
};

url.redirectTo = params => {
	uni.redirectTo(processUrlParams(params));
};

url.reLaunch = params => {
	uni.reLaunch(params);
};

url.switchTab = params => {
	uni.switchTab(params);
};

url.navigateBack = params => {
	uni.navigateBack(params);
};

export { url };
